//index.js
const urlAdd = require('../../utils/config').urlAdd;
const getUserAccountUrl = require('../../utils/config').getUserAccountUrl;
const unlossCardUrl = require('../../utils/config').unlossCardUrl;
const lossCardUrl = require('../../utils/config').lossCardUrl;
const findNoticeListUrl  = require('../../utils/config').findNoticeListUrl;
//获取应用实例
const app = getApp()
Page({
  data: {
    //九宫格菜单
    grids: [
      {
        url:'../chongzhi/chongzhi',
        spanClass:'iconfont icon-chongzhi2',
        spanColor:'#ff5167',
        text:'账户充值',
        bindtap:''
      },
   
      {
        url: '',
        spanClass: 'iconfont icon-reporttheloss',
        spanColor: '#00ec95',
        text: '挂失',
        bindtap: 'guashiCard'
      },
      {
        url: '',
        spanClass: 'iconfont icon-Solutiontohang',
        spanColor: '#5aa4ff',
        text: '解挂',
        bindtap: 'jieguaCard'
      },
      {
        url: '../mimaxiugai/mimaxiugai',
        spanClass: 'iconfont icon-mimaxiugai',
        spanColor: '#46c34d',
        text: '密码修改',
        bindtap: 'updatePwd'
      },
      {
        url: '../newsList/newsList',
        spanClass: 'iconfont icon-06',
        spanColor: '#fade4f',
        text: '通知公告',
        bindtap: ''
      },
      {
        url:'',
        spanClass:'iconfont icon-icon',
        spanColor:'#9f73ee',
        text:'更多',
        bindtap:'btn'
      },
    ],

    imagesAddress: urlAdd,  //轮播图前缀地址
    imgUrls: [
      '1.jpg',
      '4.jpg',
      '5.jpg'
    ],
    // imgheights: [],
    // current: 0,
    
    //点击更多显示弹出框
    showModal: false,

    //查询条件
    // stuCode: '20191216',
    stuCode: wx.getStorageSync('stuCode'),

    //账户余额
    userBalance: '',

    isHide: false,
    open_id: '',

    //卡片数量
    cardNum: 0,

    //新闻列表（滚动新闻）
    focusPicture: []

  },
  // 轮播图
  imgload: function (e) {
    console.log(e);
    var newsId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/newsList-child/newsList-child?id='+ newsId,
    })
  },

  //加载轮播图
  getFocusPicture: function(){
    var that = this;
    wx.request({
      url: findNoticeListUrl,
      method: 'POST',
      header: {
        'Authorization':'Bearer '+ wx.getStorageSync('token')
      },
      success: function (res) {
        
        if(res.data.code == 200){
         
          that.setData({
            imgUrls: res.data.rows
          })
        }
        
      },
    });
  },

  //页面加载函数
  yemianLoading: function (options) {
    //首页引入底部导航栏调用 app.js 里的方法
    var that = this;
    wx.showLoading({
      title: '数据加载中...',
    })
    that.findUserBalance();
  },

  //查询卡余额
  findUserBalance: function(){
    wx.hideLoading(); //隐藏加载
    var that = this;
    wx.request({
      url: getUserAccountUrl,
      method: 'POST',
      header: {
        'Authorization':'Bearer '+ wx.getStorageSync('token')
      },
      success: function (res) {
        
        if(res.data.code == 200){
          that.setData({
            userBalance: res.data.data.balance
          })
        }
        /*if(res.data.code == 401){
          wx.redirectTo({
            url: '/pages/userlogin/login'
          })
        }*/
        
      },
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    app.editTabbar();
  
  },


  // 外面的弹窗
  btn: function () {
    this.setData({
      showModal: true
    })
  },

  // 禁止屏幕滚动
  preventTouchMove: function () {},

  // 弹出层里面的弹窗
  ok: function () {
    this.setData({
      showModal: false
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    that.getFocusPicture();
    if (wx.getStorageSync('token') != '') {
      that.yemianLoading();
    }
  },

  //解挂提交
  hangCard: function () {
    var that = this;
    wx.request({
      url: unlossCardUrl,
      header: {
        'Authorization':'Bearer '+ wx.getStorageSync('token')
      },
      method: "POST",      
      success: function (result) {
        if (result.data.code == 200) {
          wx.showModal({
            title: '提示',
            content: result.data.msg,
            showCancel: false,
          })
        }
        if (result.data.code == 906) {
          wx.showModal({
            title: '提示',
            content: result.data.message,
            showCancel: false,
          })
        }
        if (result.data.code == 902) {
          wx.showModal({
            title: '提示',
            content: result.data.message,
            showCancel: false,
          })
        }
        if (result.data.code == 904) {
          wx.showModal({
            title: '提示',
            content: result.data.message,
            showCancel: false,
          })
        }

      },fail: function () {
        wx.showModal({
          title: "",
          content: "网络请求超时",
          showCancel: false,
          confirmColor: '#30a1db'
        });
      }
    })
  },
  //解挂卡
  jieguaCard: function () {
    var that = this;
    if (wx.getStorageSync('openId') != '') {
      if (wx.getStorageSync('token') == '' || wx.getStorageSync('token') == null) {
        wx.showToast({
          title: '你已授过权, 请登录',
        })
        wx.redirectTo({
          url: '/pages/login/login'
        })
      } else {
        wx.showModal({
          title: '提示',
          content: '解挂后卡将可以使用，确定解挂？',
          success: function (res) {
            if (res.confirm) {
              that.hangCard()
            }
          }
        })
      }
    } else {
      wx.showModal({
        title: '提示',
        content: '你还未授权登录，是否授权登录',
        cancelText: '取消',
        cancelColor: 'red',
        confirmText: '确认',
        confirmColor: '#30a1db',
        success: function (res) {
          //点击取消
          if (res.cancel) {
            // wx.switchTab({
            //   url: '/pages/index/index',
            // })
          } else {
            //点击确定
            wx.redirectTo({
              url: '/pages/userlogin/login'
            })
          }

        },
        fail: function () {}
      })
    }
  },

  //挂失提交
  lossCard: function () {
    var that = this;
    wx.request({
      url: lossCardUrl,
      header: {
        'Authorization':'Bearer '+ wx.getStorageSync('token')
      },
      method: "POST",     
      success: function (result) {
        console.log(result);
        if (result.data.code == 200) {
          wx.showModal({
            title: '提示',
            content: result.data.msg,
            showCancel: false,
          })
        }
        if (result.data.code == 905) {
          wx.showModal({
            title: '提示',
            content: result.data.message,
            showCancel: false,
          })
        }
        if (result.data.code == 901) {
          wx.showModal({
            title: '提示',
            content: result.data.message,
            showCancel: false,
          })
        }
        if (result.data.code == 904) {
          wx.showModal({
            title: '提示',
            content: result.data.message,
            showCancel: false,
          })
        }
      },
      fail: function () {
        // wx.showModal({
        //   title: "",
        //   content: "网络请求超时",
        //   showCancel: false,
        //   confirmColor: '#30a1db'
        // });
      }
    })
  },
  //挂失卡
  guashiCard: function () {
    var that = this;

    if (wx.getStorageSync('openId') != '') {
      if (wx.getStorageSync('token') == '' || wx.getStorageSync('token') == null) {
        wx.showToast({
          title: '你已授过权, 请登录',
        })
        wx.redirectTo({
          url: '/pages/login/login'
        })
      } else {
        wx.showModal({
          title: '提示',
          content: '挂失后将卡将无法使用，确定挂失？',
          success: function (res) {
            if (res.confirm) {
              that.lossCard()
            }
          }
        })
      }
    } else {
      wx.showModal({
        title: '提示',
        content: '你还未授权登录，是否授权登录',
        cancelText: '取消',
        cancelColor: 'red',
        confirmText: '确认',
        confirmColor: '#30a1db',
        success: function (res) {
          //点击取消
          if (res.cancel) {
            // wx.switchTab({
            //   url: '/pages/index/index',
            // })
          } else {
            //点击确定
            wx.redirectTo({
              url: '/pages/userlogin/login'
            })
          }

        },
        fail: function () {}
      })
    }

  },
  updatePwd:function(){
    if (wx.getStorageSync('openId') != '') {
    if (wx.getStorageSync('token') == '' || wx.getStorageSync('token') == null) {
      wx.showToast({
        title: '你已授过权, 请登录',
      })
      wx.redirectTo({
        url: '/pages/login/login'
      })
    }
    wx.redirectTo({
      url: '/pages/mimaxiugai/mimaxiugai'
    })
  }},

    /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '火狐一卡通',
      path: '/pages/index/index',
      imageUrl: '',
      success: function(res) {}
    }
  },
})